﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace LeadCRM.Controllers
{
    
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            if (Request.IsAuthenticated)
            {
                return RedirectToAction("HomePage", "Home");
            }
            return View();
        }
        [Authorize]
        public ActionResult HomePage()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Models.Login model)
        {
            var result = WebSecurity.Login(model.Username, model.Password, true);
            if (result)
            {
                return RedirectToAction("HomePage");
            }
            else
            {
                ViewBag.Msg = "Invalid User Name or Password";

            }
            //Models.LeadCRMEntities db = new Models.LeadCRMEntities();
            //var result = from u in db.Users
            //             where u.UserName == model.Username &&
            //             u.Password == model.Password
            //             select u;
            //if (result.Count() > 0)
            //{
            //    Session["Username"] = model.Username;
                
            //    ViewBag.Msg = "User found";
                
                
            //}
            //else
            //{
            //    ViewBag.Msg = "User not found";
            //}

            
            return View();
        }

        
        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        
    }
}
